package cn.edu.scau.cmi.lvmingyan.spring.dao;


import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.scau.cmi.lvmingyan.hibernate.domain.*;

public class TeacherDAO{
	private static final Logger LOGGER = LoggerFactory.getLogger(TeacherDAO.class);
	private Session session;
	
	public TeacherDAO() {}
	
	public void setSession(Session session) {
		this.session = session;
	}
	
	public void save(Teacher teacher) {
		LOGGER.debug("saving Teacher instance");
		try {
			session.save(teacher);
			LOGGER.debug("save successful");
		}catch (RuntimeException e) {
			LOGGER.error("save failed",e);
			throw e;
		}
	}
	
	public void update(Teacher teacher) {
		LOGGER.debug("updating Teacher instance");
		try {
			session.merge(teacher);
			LOGGER.debug("update successful");
		}catch (RuntimeException e) {
			LOGGER.error("update failed",e);
			throw e;
		}
	}
	
	public void delete(Teacher teacher) {
		LOGGER.debug("delete Teacher instance");
		try {
			Teacher t = (Teacher) session.load(Teacher.class, teacher.getId());
			session.delete(t);
			LOGGER.debug("delete successful");
		}catch (RuntimeException e) {
			LOGGER.error("delete failed");
			throw e;
		}
	}
	
}
